﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.ComponentModel;
using System.Data;

namespace EasyClass {
	public class AllAnswer {
		public Dictionary<string, string[]> answers;  //idex: stuID
		public Task task;

		public AllAnswer(Task t) {
			task = t;
			answers = new Dictionary<string, string[]>();
		}

		public Panel GetProblemAnalysisPanel(int index, string answer, int total, int nCorrect, int[] choiceCount) {
			FlowLayoutPanel flp = new FlowLayoutPanel();
			flp.FlowDirection = FlowDirection.LeftToRight;
			flp.AutoSize = true;

			Label label = new Label();
			label.AutoSize = true;
			label.Width = 100;
			label.Text = "Problem " + index.ToString() + "\n  Answer: " + answer;
			label.Text += "\n  Total Submission: " + total.ToString() + "\n  # of Correct: " + nCorrect.ToString();
			if (total == 0) ++total;
			label.Text += "\n  Correctness: " + ((int)(0.5 + (double)nCorrect*100 / total)).ToString() + "%";
			flp.Controls.Add(label);

			Chart mainChart = new Chart();
			mainChart.Titles.Add("Choice Distribution");
			ChartArea mainChartArea = new ChartArea();
			mainChartArea.AxisX.MajorGrid.Enabled = false;
			mainChartArea.AxisY.MajorGrid.Enabled = false;
			mainChartArea.AxisX.Interval = 1;
			mainChartArea.AxisY.Interval = 1;
			mainChartArea.AxisX.Title = "Choices";
			mainChartArea.AxisY.Title = "# of People Selected";
			mainChartArea.AxisY.Minimum = 0;
			Series mainSeries = new Series();
			mainSeries.ChartArea = mainChartArea.Name;
			mainSeries.ChartType = SeriesChartType.Column;
			mainSeries.IsValueShownAsLabel = true;
			mainSeries.SmartLabelStyle.Enabled = false;
			for (int i = 0; i < choiceCount.Length; ++i)
				mainSeries.Points.AddXY(((char)('A' + i)).ToString(), choiceCount[i]);
			mainChart.ChartAreas.Add(mainChartArea);
			mainChart.Series.Add(mainSeries);
			flp.Controls.Add(mainChart);

			return flp;
		}

		public void FreshPage_Click(object sender, EventArgs e) {
			Controller.GetController.UpdateAnalysisPanel(this);
		}

		public Panel GetPanel() {
			FlowLayoutPanel flp = new FlowLayoutPanel();
			flp.FlowDirection = FlowDirection.TopDown;
			flp.AutoSize = true;

			Button fresh = new Button();
			fresh.AutoSize = true;
			fresh.Click += new EventHandler(FreshPage_Click);
			fresh.Text = "Fresh the Page";
			flp.Controls.Add(fresh);

			int nProblems = task.problems.Count;
			int[] numCount = new int[nProblems+1];
			foreach (string stuID in answers.Keys) {
				string[] choices = answers[stuID];
				int cnt = 0;
				for (int i = 0; i < nProblems; ++i)
					if (choices[i] == task.problems.ElementAt(i).answer)
						++cnt;
				++numCount[cnt];
			}
			MyConsole.WriteLine("Total number of problems: " + numCount.ToString());

			Chart mainChart = new Chart();  //paint line figure
			mainChart.Titles.Add("Correctness Statistics");
			ChartArea mainChartArea = new ChartArea();
			mainChartArea.AxisX.MajorGrid.Enabled = false;
			mainChartArea.AxisY.MajorGrid.Enabled = false;
			mainChartArea.AxisX.Interval = 1;
			mainChartArea.AxisY.Interval = 1;
			mainChartArea.AxisX.Title = "# of Correct Problems";
			mainChartArea.AxisY.Title = "# of People";
			mainChartArea.AxisX.Minimum = 0;
			mainChartArea.AxisY.Minimum = 0;
			Series mainSeries = new Series();
			mainSeries.ChartArea = mainChartArea.Name;
			mainSeries.ChartType = SeriesChartType.Line;
			mainSeries.IsValueShownAsLabel = true;
			mainSeries.SmartLabelStyle.Enabled = false;
			for (int i = 0; i <= nProblems; ++i)
				mainSeries.Points.AddXY(i, numCount[i]);
			mainChart.ChartAreas.Add(mainChartArea);
			mainChart.Series.Add(mainSeries);
			flp.Controls.Add(mainChart);

			for (int i = 0; i < nProblems; ++i) {
				Problem problem = task.problems.ElementAt(i);
				int[] choiceCount = new int[problem.choices.Length];
				int nCorrect = 0;
				foreach (string[] ss in answers.Values) {
					if (ss[i] == problem.answer) ++nCorrect;
					for (int j = 0; j < ss[i].Length; ++j)
						++choiceCount[ss[i][j] - 'A'];
				}
				flp.Controls.Add(GetProblemAnalysisPanel(i+1, problem.answer, answers.Count, nCorrect, choiceCount));
			}

			return flp;
		}

		public bool TrySubmitAnswer(string studentID, string[] answer, ref string reason) {
			if (answers.ContainsKey(studentID)) {  //has submitted
				reason = "Duplicate submission";
				return false;
			} else if (task.status == 2) {  //task has finished
				reason = "Time expired";
				return false;
			}
			answers[studentID] = answer;
			return true;
		}

		public void CreateTxtLog(string path) {
			StreamWriter sr = File.CreateText(path);
			sr.WriteLine("TaskName: {0}, Date: {1}", task.taskName, DateTime.Now.ToShortDateString());
			sr.WriteLine("");

			sr.Write("{0,-15}", "StudentID");
			for (int i = 0; i < task.problems.Count; ++i) {
				sr.Write("{0,-10}", "Problem" + (i+1).ToString());
			}
			sr.WriteLine("");

			sr.Write("{0,-15}", "Standard Ans");
			for (int i = 0; i < task.problems.Count; ++i) {
				sr.Write("{0,-10}", task.problems.ElementAt(i).answer);
			}
			sr.WriteLine("");

			foreach(string studentID in answers.Keys) {
				sr.Write("{0,-15}", studentID);
				for (int i = 0; i < answers[studentID].Length; ++i) {
					sr.Write("{0,-10}", answers[studentID][i]);
				}
				sr.WriteLine("");
			}
			sr.WriteLine("");

			sr.Close();
		}
	}
}
